package com.slkj.shilixiaoyuanapp.ui.user.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.CollectEntity;
import com.slkj.shilixiaoyuanapp.entity.FileEntity;
import com.slkj.shilixiaoyuanapp.entity.MyFileEntity;
import com.slkj.shilixiaoyuanapp.entity.code.FileCode;
import com.slkj.shilixiaoyuanapp.ui.common.BrowseFilesActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.functions.Action;


@ActivityInfo(layout = R.layout.my_files_activity, title = "我的文件")
public class MyFilesActivity extends BaseActivity {
    @BindView(R.id.statelayout)
    StateLayout stateLayout;

    @BindView(R.id.recycer)
    RecyclerView recycer;
    BaseQuickAdapter adapter;
    List<MyFileEntity> datas = new ArrayList<>();

    @Override
    protected void init() {
        adapter = new BaseQuickAdapter<MyFileEntity, BaseViewHolder>(R.layout.item_my_file_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final MyFileEntity item) {
                helper.setText(R.id.tv_name, item.getName())
                        .setText(R.id.tv_size, item.getFileSize())
                        .setText(R.id.tv_time, item.getTime())
                        .itemView.setOnClickListener(v -> {
                    BrowseFilesActivity__JumpCenter.builder(MyFilesActivity.this).setFilePath(item.getPath())
                            .setFileName(item.getName()).create().go();
                });
            }
        };
        recycer.setLayoutManager(new LinearLayoutManager(this));
        recycer.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
        recycer.setAdapter(adapter);
        stateLayout.setOnReLoadListener(this::getData);
        getData();
    }

    private void getData() {
        Observable.create(emitter -> {
                    File file = new File(FileCode.MY_FILE);
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (int i = 0; i < files.length; i++) {
                            datas.add(new MyFileEntity(files[i].getName(), files[i].getPath(), FileEntity.FileSizeUtil.getAutoFileOrFilesSize(files[i].getPath()), TimeUtils.longToStr(files[i].lastModified())));
                        }
                    }
                    emitter.onComplete();
                })
                .compose(getThread())
                .doOnComplete(() -> {
                    if (datas.isEmpty()) {
                        stateLayout.showEmptyView();
                    } else {
                        adapter.notifyDataSetChanged();
                        stateLayout.showContentView();
                    }
                })
                .subscribe();
    }


}
